package com.li.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author liXingaYu
 * @create 2022-03-16 20:08
 */
@Component//加载项目的时候加载
@Slf4j  //日志
public class MyMeteObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("insert的时候字动填充");

        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());

        //在metaObject中用getFieldValByName查询是否有name属性
        Object name = this.getFieldValByName("name", metaObject);
        if (name == null){
            log.info("name自动填充");
            this.strictInsertFill(metaObject,"name",String.class,"李白");
        }

//        Object detele = this.getFieldValByName("deleted", metaObject);
//        if (detele == null){
//            log.info("deleted");
//            this.strictInsertFill(metaObject,"deleted",Integer.class,0);
//        }

        //hasSetter判断该anthor属性是否存在
//        boolean anthor = metaObject.hasSetter("anthor");
//        if (anthor){
//            log.info("没有的属性");
//            this.strictInsertFill(metaObject,"anthor",String.class,"白痴");
//        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("update的时候字动填充");

        this.strictUpdateFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());

    }
}
